package entity.text;

import entity.file.AbstractFile;
import entity.file.FileDecorator;
import entity.file.PdfFile;
import entity.file.TxtFile;
import util.JDBCUtil;

import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;

/**
 * @author ：mmzs
 * @date ：Created in 2020/9/16 23:30
 * @description：
 * @modified By：
 * @version: $
 */
@Deprecated
public class Chapter implements Text {

    public String index;

    public String subTitle;

    public String content;


    public static void main(String[] args) {
        Map<Integer, Paper> map = new HashMap<>();
        //从数据库和本地磁盘加载引用链文章的信息，放在map里
        FileDecorator fileDecorator = new FileDecorator();
        String location = "G:\\txtpaper\\ai\\";
        Connection connection = JDBCUtil.getConnection();
//        String sql = "SELECT id, url FROM paper WHERE id = 2562";
        String sql = "SELECT paper.`id`, url FROM CHAIN, paper WHERE chain.`id` = paper.`id`";
        try {
            Statement statement = connection.createStatement();
            ResultSet resultSet1 = statement.executeQuery(sql);
            while (resultSet1.next()) {
                AbstractFile abstractFile = null;
                int id = resultSet1.getInt(1);
                String url = resultSet1.getString(2);
                String[] split = url.split("/");
                String fileName = split[split.length-1] + ".txt";
                String filePath = location + fileName;
                File file = new File(filePath);
                if(file.exists()) {
                    abstractFile = new TxtFile(id, file);
                    //传给下一层
                } else {
                    filePath = filePath.replace("txt", "pdf");
                    file = new File(filePath);
                    if (file.exists()) {
                        abstractFile = new PdfFile(id, file);
                    } else {
                        System.out.println(fileName + "  not found");
                        continue;
                    }
                }

                abstractFile = fileDecorator.doDecorator(abstractFile);
                Paper paper = new Paper(abstractFile);
                paper.extractChapters();
                map.put(paper.id, paper);

            }

            Iterator<Map.Entry<Integer, Paper>> iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<Integer, Paper> next = iterator.next();
                System.out.println("==============" + next.getKey() + "============");
                next.getValue().chapters.forEach(a -> {
                    System.out.println("" + a.index + " " + a.subTitle);
                });
                System.out.println("============================================");
            }

//            看看map哪些文章有异常: 2562,2275,3045,2803,2172

            Scanner scanner = new Scanner(System.in);
            System.out.println("+++++++++++++++++++++++++++");
            while (true) {
                int id = Integer.valueOf(scanner.nextLine());
                String words = scanner.nextLine();

                Paper paper = map.get(id);
                for (int i = 1; i < 20; i++) {
                    String index = Integer.toString(i);
                    String subTitle = "";
                    StringBuilder stringBuilder = new StringBuilder();
                    for (Chapter chapter : paper.chapters) {
                        if (chapter.index.equals(index)) {
                            subTitle = chapter.subTitle;
                        }
                        if (chapter.index.startsWith(index)) {
                            stringBuilder.append(chapter.content);
                        }
                    }
                    if (stringBuilder.length() != 0) {
                        String s = stringBuilder.toString().replace("\n", "");
                        int times = findString(s, words);
                        System.out.println(index + " " + subTitle + times / (0.0 + s.split(" ").length));
                        System.out.println(times + "次");
                    }
                }

            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static int findString(String source, String target){
        //计数器
        int count = 0;
        //每次查询开始的位置
        int fromIndex = 0;
        while((fromIndex = source.indexOf(target, fromIndex)) != -1){
            count++;
            fromIndex = fromIndex + target.length();
        }
        return count;
    }


}
